Run in terminal: pip install lifetimes
Original link at Github: https://github.com/CamDavidsonPilon/lifetimes
In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import lifelines
In [2]:
%pylab inline
pylab.rcParams['figure.figsize'] = (14, 9)
In [3]:
from lifetimes.datasets import load_cdnow
In [4]:
data = load_cdnow(index_col=[0])
In [5]:
data.head()
Out[5]:
In [6]:
from lifetimes import BetaGeoFitter
In [7]:
bgf = BetaGeoFitter(penalizer_coef=0.0)
In [8]:
bgf.fit(data['frequency'], data['recency'], data['T'])
Out[8]:
In [9]:
from lifetimes.plotting import plot_frequency_recency_matrix
In [10]:
plot_frequency_recency_matrix(bgf)
Out[10]:
In [11]:
from lifetimes.plotting import plot_probability_alive_matrix
In [12]:
plot_probability_alive_matrix(bgf)
Out[12]:
In [13]:
t = 1
In [14]:
data['predicted_purchases'] = bgf.conditional_expected_number_of_purchases_up_to_time(t, data['frequency'], data['recency'], data['T'])
In [15]:
data.sort_values(by='predicted_purchases').tail(5)
Out[15]:
In [16]:
from lifetimes.plotting import plot_period_transactions
In [17]:
plot_period_transactions(bgf)
Out[17]:
In [18]:
from lifetimes.datasets import load_transaction_data
from lifetimes.utils import summary_data_from_transaction_data
In [19]:
transaction_data = load_transaction_data()
In [20]:
transaction_data.head()
Out[20]:
In [21]:
summary = summary_data_from_transaction_data(transaction_data, 'id', 'date', observation_period_end='2014-12-31')
In [22]:
summary.head()
Out[22]:
In [23]:
bgf.fit(summary['frequency'], summary['recency'], summary['T'])
Out[23]:
In [24]:
from lifetimes.utils import calibration_and_holdout_data
In [25]:
summary_cal_holdout = calibration_and_holdout_data(transaction_data, 'id', 'date',
calibration_period_end='2014-09-01',
observation_period_end='2014-12-31' )
In [26]:
summary_cal_holdout.head()
Out[26]:
In [27]:
from lifetimes.plotting import plot_calibration_purchases_vs_holdout_purchases
In [28]:
bgf.fit(summary_cal_holdout['frequency_cal'], summary_cal_holdout['recency_cal'], summary_cal_holdout['T_cal'])
Out[28]:
In [29]:
plot_calibration_purchases_vs_holdout_purchases(bgf, summary_cal_holdout)
Out[29]:
In [30]:
t = 10 #predict purchases in 10 periods
In [31]:
individual = summary.iloc[20]
In [32]:
# The below function is an alias to `bfg.conditional_expected_number_of_purchases_up_to_time`
bgf.predict(t, individual['frequency'], individual['recency'], individual['T'])
# 0.0576511
Out[32]:
In [33]:
from lifetimes.plotting import plot_history_alive
In [34]:
id = 35
days_since_birth = 200
In [35]:
sp_trans = transaction_data.ix[transaction_data['id'] == id]
In [36]:
plot_history_alive(bgf, days_since_birth, sp_trans, 'date')
Out[36]:
In [37]:
from lifetimes.datasets import load_summary_data_with_monetary_value
In [38]:
summary_with_money_value = load_summary_data_with_monetary_value()
In [39]:
summary_with_money_value.head()
Out[39]:
In [40]:
returning_customers_summary = summary_with_money_value[summary_with_money_value['frequency']>0]
In [41]:
returning_customers_summary.head()
Out[41]:
In [42]:
returning_customers_summary[['monetary_value', 'frequency']].corr()
Out[42]:
In [43]:
from lifetimes import GammaGammaFitter
In [44]:
ggf = GammaGammaFitter(penalizer_coef = 0)
In [45]:
ggf.fit(returning_customers_summary['frequency'],
returning_customers_summary['monetary_value'])
Out[45]:
In [46]:
print (ggf)
In [47]:
print (ggf.conditional_expected_average_profit(
summary_with_money_value['frequency'],
summary_with_money_value['monetary_value']
).head())
In [48]:
print ("Expected conditional average profit: %s, Average profit: %s" % (
ggf.conditional_expected_average_profit(
summary_with_money_value['frequency'],
summary_with_money_value['monetary_value']
).mean(),
summary_with_money_value[summary_with_money_value['frequency']>0]['monetary_value'].mean()
))
In [49]:
bgf.fit(summary_with_money_value['frequency'], summary_with_money_value['recency'], summary_with_money_value['T'])
Out[49]:
In [50]:
print (ggf.customer_lifetime_value(
bgf, #the model to use to predict the number of future transactions
summary_with_money_value['frequency'],
summary_with_money_value['recency'],
summary_with_money_value['T'],
summary_with_money_value['monetary_value'],
time=12, # months
discount_rate=0.01 # monthly discount rate ~ 12.7% annually
).head(10))
In [ ]: